<!--    客户管理-添加客户--机构客户  by jh   -->
<template>
    <div>
        <div class="right ca-p-t-50">
            <el-row>
                <el-col :span="24" class="ca-align-right">
                    <div class="ca-head-fixed">
                        <el-button type="primary" @click="save()">保存</el-button>
                        <el-button @click="back()">退出</el-button>
                    </div>
                </el-col>
            </el-row>
                <el-form  label-width="150px" :model="guestDTO" :rules="rules" ref="ruleForm">
                    <div class="ca-box ca-p-20">
                        <h3 class="tab-pane-title">客户关系</h3>
                        <el-form-item label="客户关系：" required>
                            <el-checkbox-group class="clientRep"  v-model="guestRelationship" @change="changeCustomerRelationship">
                                <el-checkbox
                                    v-for="(item,index) in guestRelationshipAll"
                                    :label="item.name"
                                    :key="index">
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="归属部门：">
                                    <el-select
                                        @change="deptChange"
                                        class="selectCommonStyle"
                                        v-model="guestDTO.listDepartment.departmentId"
                                        placeholder="请选择归属部门">
                                        <el-option
                                            v-for="(item,index) in belongingDepartmentAll"
                                            :key="index"
                                            :label="item.deptName"
                                            :value="item.deptId">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="归属员工A：">
                                    <el-select class="selectCommonStyle" v-model="guestDTO.listStaff.staffIdA" placeholder="请选择归属员工">
                                        <el-option
                                            v-for="(item,index) in belongingStaffAllFilter"
                                            :key="index"
                                            :label="item.userName"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="客户等级：">
                                    <el-select  class="selectCommonStyle"  v-model="guestDTO.listGuest.guestLevel" placeholder="请选择客户等级">
                                        <el-option
                                            v-for="(item,index) in guestGradeAll"
                                            :key="index"
                                            :label="item.name"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="归属员工B：">
                                    <el-select class="selectCommonStyle" v-model="guestDTO.listStaff.staffIdB" placeholder="请选择归属员工">
                                        <el-option
                                            v-for="(item,index) in belongingStaffAllFilter"
                                            :key="index"
                                            :label="item.userName"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="ca-box ca-p-20 ca-m-t-20">
                        <h3 class="tab-pane-title">访问权限</h3>
                        <el-row class="visitAuthStyle ca-m-t-10">
                            <el-col :span="5">
                                <!--<el-radio-group v-model="guestDTO.listGuest.isAll" @change="accessRightsChange">-->
                                <!--<el-radio :label="'1'">全体员工</el-radio>-->
                                <!--<el-radio :label="'2'">指定员工</el-radio>-->
                                <!--</el-radio-group>-->
                                <el-checkbox v-model="isAll" @change="accessRightsChange">全体员工</el-checkbox>
                                <el-checkbox v-model="isAppoint" @change="accessRightsChange2">指定员工</el-checkbox>
                            </el-col>
                            <el-col :span="11" style="color:#409EFF;" v-show="isAppoint">
                                (注：可直接输入员工姓名或者通过部门选择)
                            </el-col>
                        </el-row>
                        <el-row v-show="isAppoint">
                            <el-col>
                                <el-row>
                                    <el-form-item label="输入员工姓名：" >
                                        <el-select v-model="searchStaff" filterable placeholder="模糊匹配员工姓名"
                                                   @change="addPowerPerson">
                                            <el-option
                                                v-for="item in belongingStaffAll"
                                                :disabled="item.disabled"
                                                :label="item.userName"
                                                :key="item.id"
                                                :value="item.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-row>
                                <el-row>
                                    <el-form-item label="输入部门员工：" >
                                        <el-select v-model="searchDept" filterable placeholder="模糊匹配部门员工"
                                                   @change="addPowerDept">
                                            <el-option
                                                v-for="(item,index) in belongingDepartmentAll"
                                                :label="item.deptName"
                                                :key="index"
                                                :value="item.deptId">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-row>
                                <el-row class="tagStyle">
                                    <el-tag
                                        class="tagStyle-tag"
                                        @close="handleCloseTag(tag)"
                                        v-for="tag in powerUList"
                                        :key="tag.id"
                                        closable
                                        type="">
                                        {{tag.userName}}
                                    </el-tag>
                                </el-row>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="ca-box ca-p-20 ca-m-t-20">
                        <h3 class="tab-pane-title">基础资料</h3>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="机构名称："  prop="guestName">
                                    <el-input v-model="guestDTO.guestName" placeholder="请输入机构名称" :maxlength="100" ></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="法定代表人：" >
                                    <el-input v-model="guestDTO.guestAgencyBasic.legalPerson" placeholder="请输入法定代表人" :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="机构性质：">
                                    <el-input v-model="guestDTO.guestAgencyBasic.properties" placeholder="请输入机构性质" :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="成立时间：">
                                    <!--<el-input v-model="guestDTO.guestAgencyBasic.setupTime" placeholder="成立时间"  :maxlength="100"></el-input>-->
                                    <el-date-picker
                                        class="selectCommonStyle"
                                        v-model="guestDTO.guestAgencyBasic.setupTime"
                                        type="date" >
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <csRegion labelWidth="150px" :title="'注册地址：'" :address="registeredAddress"></csRegion>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <csRegion labelWidth="150px" :title="'办公地址：'"  :address="officeAddress"></csRegion>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12"  >
                                <el-form-item label="注册资本（万元）："  prop="registeredCapital">
                                    <el-input v-model="guestDTO.guestAgencyBasic.registeredCapital" placeholder="请输入注册资本" :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" >
                                <el-form-item label="实收资本（万元）："  prop="capitalCollection">
                                    <el-input v-model="guestDTO.guestAgencyBasic.capitalCollection" placeholder="请输入实收资本" :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="ca-box ca-p-20 ca-m-t-20 ca-table-add-reduce">
                        <h3 class="tab-pane-title">高管</h3>
                        <el-table
                            class="ca-table ca-m-t-10"
                            :data="executivesList"
                            @header-click="addRowEvent"
                            >
                            <el-table-column
                                prop="executivesName"
                                label="高管姓名"
                                >
                                <template slot-scope="scope">
                                   <el-input v-model="scope.row.executivesName" placeholder="请输入高管姓名"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="gender"
                                label="性别"
                               >
                                <template slot-scope="scope">
                                    <el-select  class="selectCommonStyle"  v-model="scope.row.gender" >
                                        <el-option
                                            v-for="(item,index) in customSex"
                                            :key="index"
                                            :label="item.name"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="post"
                                label="职位">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.post" placeholder="请输入职位"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="phone"
                                label="电话">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.phone" placeholder="请输入电话"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="mail"
                                label="邮箱">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.mail" placeholder="请输入邮箱"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="fax"
                                label="传真">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.fax" placeholder="请输入传真"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column
                                prop="remark"
                                label="备注">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.remark" type="textarea"  placeholder="请输入备注" :maxlength="300"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column
                                label-class-name="add-button"
                                label="+">
                                <template slot-scope="scope">
                                    <el-button class="reduce-button" @click.native="handleDelete(scope.$index,executivesList)">
                                        <i class="fa fa-times"></i>
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                    <div class="ca-box ca-p-20 ca-m-t-20">
                        <h3 class="tab-pane-title">主要联系人</h3>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="联系人">
                                    <el-input v-model="guestDTO.listBasic.contact" placeholder="请输入联系人"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="性别：">
                                    <el-select  class="selectCommonStyle"  v-model="guestDTO.listBasic.gender" placeholder="请选择性别">
                                        <el-option
                                            v-for="(item,index) in customSex"
                                            :key="index"
                                            :label="item.name"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="联系电话：" prop="phone">
                                    <el-input v-model="guestDTO.listBasic.phone" placeholder="请输入联系电话"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="E-mail：" prop="mail">
                                    <el-input v-model="guestDTO.listBasic.mail" placeholder="请输入E-mail"  :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="通信地址：">
                                    <el-input v-model="guestDTO.listBasic.address" placeholder="请输入通信地址"  :maxlength="100"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="邮编：" prop="code">
                                    <el-input v-model="guestDTO.listBasic.code" placeholder="请输入邮编"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>
            </div>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from '__views/customer-management/api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    export default {
        data() {
            let validatePhone = (rule, value, callback) => {
                value = this.guestDTO.listBasic.phone
                if (value === '' || value === null) {
                    callback();
                } else {
                    if (!(/^1(3|4|5|7|8)\d{9}$/.test(value))) {
                        callback(new Error('请输入正确的移动电话'));
                    } else {
                        callback();
                    }
                }
            };
            let validateMail = (rule, value, callback) => {
                value = this.guestDTO.listBasic.mail
                if (value === '' || value === null) {
                    callback();
                } else {
                    if (!((/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test(value))) {
                        callback(new Error('请输入正确的邮箱地址'));
                    } else {
                        callback();
                    }
                }
            };
            let validateCode = (rule, value, callback) => {
                value = this.guestDTO.listBasic.code
                if (value === '' || value === null) {
                    callback();
                } else {
                    if (!(/^[1-9][0-9]{5}$/.test(value))) {
                        callback(new Error('请输入正确的邮编'));
                    } else {
                        callback();
                    }
                }
            };
            let validateNumber = (rule, value, callback) => {
                value = this.guestDTO.guestAgencyBasic.registeredCapital
                var reg = /^\d{1,10}(\.\d{1,10})?$/;
                if (value && (!reg.test(value))) {
                    callback(new Error('请输入正确范围的数字'));
                } else {
                    callback();
                }
            };
            let validateNumber2 = (rule, value, callback) => {
                value = this.guestDTO.guestAgencyBasic.capitalCollection
                var reg = /^\d{1,10}(\.\d{1,10})?$/;
                if (value && (!reg.test(value))) {
                    callback(new Error('请输入正确范围的数字'));
                } else {
                    callback();
                }
            };
            return {
                checkFlag: true, //高管检验通过标志
                isAll: false, //全体员工
                isAppoint: false, //指定员工
                executivesList: [
//                    {
//                        'createUserId': this.$store.state.user.id,
//                        'executivesName': '',//姓名 ,
//                        'fax': '',//传真 ,
//                        'gender': null,//性别 ,int
//                        'guestId': '',//客户id ,
//                        'id': '',
//                        'mail': '', //邮箱 ,
//                        'phone': '',//电话 ,
//                        'post': '',//职位 ,
//                        'remark': '',//备注
//                    }
                ],
               //字典中的数据
                dictionaryMap: this.$store.state.user.dictionaryMap,//字典
                guestRelationshipAll: [],//客户关系
                guestGradeAll: [],//客户等级
                guestTypeAll: [],//客户类型
                guestCodeTypeAll: [],//证件类型
                belongingDepartmentAll: [],//归属部门全部
                belongingStaffAll: [],//归属员工全部
                belongingStaffAllFilter: [],//筛选后的归属员工
                customSex: [{'name': '男', 'id': 0}, {'name': '女', 'id': 1}],   //客户性别
                //需要单独二次加工的数据
                guestRelationship: [],//客户关系
                //访问权限的变量
                searchStaff: '',//查找的归属员工
                searchDept: '',//选择的归属部门
                powerUList: [],//有权限的人员列表
                registeredAddress: {
                    id: '',
                    businessId: '',
                    province: '',
                    city: '',
                    district: '',
                    businessType: 1,
                    registrationDetailed: ''
                },//注册地址
                officeAddress: {
                    id: '',
                    businessId: '',
                    province: '',
                    city: '',
                    district: '',
                    businessType: 2,
                    registrationDetailed: ''
                }, //办公地址
                //新建客户上传参数
                guestDTO: {
                    'executivesList': [],
                    'guestAgencyBasic': {
                        'agencyName': '',//机构名称
                        'deleteStauts': 0,//删除 ,int
                        'guestId': '',//客户 id ,
                        'id': '',
                        'legalPerson': '',// 法人 ,
                        'properties': '',//机构性质 ,
                        'setupTime': new Date(),//成立时间
                        'registeredCapital': null,//注册资本
                        'capitalCollection': null,//实收资本
                    },
                    'guestId': '',
                    'guestName': '',
                    'listBasic': { //基础资料 ,
                            'address': '',//地址
                            'code': '',//邮编
                            'contact': '',//联系人
                            'credentialsCode': '', //证件号码
                            'credentialsType': '', //证件类型0身份证 1护照 2其他 接口需要int
                            'gender': null,
                            'guestBirthday': new Date(), //生日
                            'guestId': '',//客户id
                            'guestName': '',//名称
                            'guestPost': '',//职务
                            'guestType': '1',//客户类型 0：个人 1：机构投资人 ,接口需要int
                            'id': '', //记录id
                            'mail': '',//邮编
                            'phone': ''//电话
                    },
                    'listDepartment': {
                        'departmentId': '',
                        'guestId': '',
                        'id': ''
                    },//归属部门 ,
                    'listGuest': { //主表 ,
                        'guestDescription': '', //宾客描述 ,
                        'guestHobby': '',//客户兴趣爱好 ,
                        'guestLevel': '',//客户等级 0：低 1：中 2：高 ,接口需要int
                        'guestName': '', //客户名称
                        'guestType': '1', //客户类型,接口需要int
                        'id': '',
                        'isAll': '2', //权限 ,
                        'other': '' //其他
                    },
                    'listRole': [],//权限
                    'listGuestType': [],//客户关系
                    'listStaff': {//归属员工
                        'guestId': '',
                        'id': '',
                        'staffIdA': '',
                        'staffIdB': ''
                    },
                    'regionEntityList': [
                        {
                            'businessId': '',
                            'businessType': 0,
                            'city': '',
                            'district': '',
                            'id': '',
                            'province': '',
                            'registrationDetailed': ''
                        }
                    ]
                },
                rules: {
                    guestName: [
                        {required: true,message: '请输入客户名称', trigger: 'blur'}
                    ],
                    mail: [
                        //{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
                        { validator: validateMail, trigger: 'blur' },
                    ],
                    phone: [
                        { validator: validatePhone, trigger: 'blur'},
                    ],
                    code: [
                        { validator: validateCode, trigger: 'blur'},
                    ],
                    registeredCapital: [
                        { validator: validateNumber, trigger: 'blur' }
                    ],
                    capitalCollection: [
                        { validator: validateNumber2, trigger: 'blur' }
                    ],
                },
                loginer: { //当前登陆者的信息
                    id: '',
                    guestId: '',
                    userId: this.$store.state.user.id,
                    userName: this.$store.state.user.cnName,
                    userDepartment: this.$store.state.user.deptId,
                }
            }
        },
        watch: {
        },
        mounted() {
            this.initView()
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.checkFlag) {
                            this.checkName()//检验客户名字是否重名
                        }
                    } else {
                        return false;
                    }
                });
            },
            //验证高管的电话、邮箱、传真
            seniorExecutiVeverification() {
                if (this.executivesList.length !== 0) {
                    for (let i = 0; i < this.executivesList.length; i++ ) {
                        if (!this.executivesList[i].executivesName) {
                            this.checkFlag = false
                            this.$message({
                                type: 'error',
                                message: '请输入高管姓名-高管列表!',
                                center: true
                            });
                            break;
                        }
                        if (this.executivesList[i].phone) {
                            if (!(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/.test(this.executivesList[i].phone))) {///^1(3|4|5|7|8)\d{9}$/
                                this.checkFlag = false
                                this.$message({
                                    type: 'error',
                                    message: '请输入正确的电话-高管列表!',
                                    center: true
                                });
                                break;
                            }
                        }
                        if (this.executivesList[i].mail) {
                            if (!((/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test(this.executivesList[i].mail))) {
                                this.checkFlag = false
                                this.$message({
                                    type: 'error',
                                    message: '请输入正确的邮箱地址-高管列表!',
                                    center: true
                                });
                                break;
                            }
                        }
                        if (this.executivesList[i].fax) {
                            if (!(/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/.test(this.executivesList[i].fax))) {
                                this.checkFlag = false
                                this.$message({
                                    type: 'error',
                                    message: '请输入正确的传真号码-高管列表!',
                                    center: true
                                });
                                break;
                            }
                        }
                    }
                }
            },
            save() {
                this.checkFlag = true //初始化字段，高管字段修改后的提交
                if (this.guestRelationship.length === 0) {
                    this.checkFlag = false
                    this.$message({
                        message: '请选择客户关系',
                        type: 'error',
                        center: true
                    });
                    return
                }
                this.seniorExecutiVeverification() //高管验证
                this.submitForm('ruleForm')
            },
            checkName() {//检验是否重名
                let params = {
                    cnName: this.guestDTO.guestName,
                    id: '',
                }
                apiHttp(api.CHECK_CLIENT_NAME,params).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) { //1代表存在数据库相同的客户名，0不存在
                        if (ret.data === 1 ) {
                            this.$confirm('客户名称已存在, 是否继续?', '提示', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                this.addClient()
                            },() => {
                                //                    this.$message({
                                //                        type: 'info',
                                //                        message: '已取消删除',
                                //                        center: true
                                //                    });
                            })
                        } else if (ret.data === 0 ) {
                            this.addClient()
                        }
                    }
                })
            },
            //删除一行事件
            handleDelete(index,data) {
                //编辑 --》删除数据库中已有的数据
                    this.$confirm('是否删除此高管信息?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        data.splice(index, 1)
                    },()=>{})
            },
            //添加一行事件
            addRowEvent(column,event) {
               if (event.target.className === 'cell add-button') {
                   let obj = {
                       'createUserId': this.$store.state.user.id,
                       'executivesName': '',//姓名 ,
                       'fax': '',//传真 ,
                       'gender': null,//性别 ,int
                       'guestId': '',//客户id ,
                       'id': '',
                       'mail': '', //邮箱 ,
                       'phone': '',//电话 ,
                       'post': '',//职位 ,
                       'remark': '',//备注
                   }
                   this.executivesList.push(obj)
               }
            },
            //初始化页面
            initView() {
                //下拉数据
                this.guestRelationshipAll = this.dictionaryMap.guestRelationship //客户关系
                this.guestRelationshipAll.sort(sortById)
                this.guestGradeAll = this.dictionaryMap.guestGrade//客户等级
                this.guestTypeAll = this.dictionaryMap.guestType //客户类型
                this.guestCodeTypeAll = this.dictionaryMap.guestCodeType //证件类型
                this.belongingDepartmentAll = this.$store.state.user.department.departmentList //全部的部门
                this.belongingStaffAll = this.$store.state.user.department.userEntityList //全部的员工
                this.belongingStaffAll.map((item)=> {
                    item.disabled = false
                })
                this.belongingStaffAllFilter = this.$store.state.user.department.userEntityList ////筛选后的归属员工--默认全部的员工
            },
            //归属部门改变时判断归属员工A、B是否为该部门 ，不是就清空
            deptChange(deptId) {
                for (let item of this.belongingStaffAll) {
                    if (item.deptId !== deptId) {
                        this.guestDTO.listStaff.staffIdA = ''
                        this.guestDTO.listStaff.staffIdB = ''
                    }
                }
                //改变归属员工的下拉显示
                let list = []
                for (let item of this.belongingStaffAll) {
                    if (item.deptId === deptId) {
                        list.push(item);
                    }
                }
                this.belongingStaffAllFilter = list
            },
            //指定员工 ---关闭授权tag
            handleCloseTag(tag) {
                this.$confirm('是否删除此员工?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.powerUList.splice(this.powerUList.indexOf(tag), 1);
                    this.searchStaff = ''
                    this.searchDept = ''
                 //去掉置灰
//                for (let item of this.belongingStaffAll) {
//                    if (tag.id === item.id) {
//                        item.disabled = false
//                    }
//                }
                },() => {
//                    this.$message({
//                        type: 'info',
//                        message: '已取消删除',
//                        center: true
//                    });
                })
            },
            //更改客户关系
            changeCustomerRelationship() {
                //console.log(value,"客户关系")
            },
            //授权人员变化
            accessRightsChange() {
                if (this.isAll) {//互斥的checked框
                    this.isAppoint = false
                }
            },
            accessRightsChange2() {
                if (this.isAppoint) {//互斥的checked框
                    this.isAll = false
                }
            },
            //访问权限=》员工姓名模糊匹配
            addPowerPerson(id) {
                //console.log(id,"员工id")
                let flag = false;
                for (let item of this.powerUList) {
                    if (item.id === id) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    for (let item of this.belongingStaffAll) {
                        if (item.id === id) {
                            //让选中的置灰
                            //item.disabled = true
                            //this.powerUList.splice(0, 0, item);
                            this.powerUList.push(item);
                        }
                    }
                }
                //this.searchStaff=''
            },
            //访问权限=》选择部门员工
            addPowerDept(deptId) {
                //console.log(deptId,'部门id')
                //把相同  部门id  的员工 都放入授权人员数组
                let arr = [...this.powerUList]
                for (let item of this.belongingStaffAll) {
                    if (item.deptId === deptId) {
                        //this.powerUList.splice(0, 0, item);
                        this.powerUList.push(item);
                    }
                }
                //去重
                for (var i = 0; i < this.powerUList.length; i++) {
                    var flag = true;
                    for (var j = 0; j < arr.length; j++) {
                        if (this.powerUList[i].id === arr[j].id) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        arr.push(this.powerUList[i]);
                    }
                }
                this.powerUList = arr
                //this.searchDept=''
            },
            //添加客户---接口
            addClient() {
                //处理数据
                let params = this.formatParam()
                //console.log(params,"新建的参数")
                apiHttp(api.ADD_CLIENT,params).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        //console.log(ret,"提交成功")
                        this.$message({
                            message: '添加机构客户成功',
                            type: 'success',
                            center: true
                        });
                        this.$router.push({ name: 'customer-management'})
                    }
                })
            },
            formatParam() {//格式化上传参数
                let that = this
                //客户关系---数据处理
                let guests = [];
                for (let item of that.guestRelationship) {
                    for (let va of that.guestRelationshipAll) {
                        if (va.name === item) {
                            guests.push({
                                'id': '',
                                'guestId': '',
                                'guestTypeId': va.id,
                            })
                        }
                    }
                }
                //访问权限员工处理
                if (this.isAll) {
                    this.guestDTO.listGuest.isAll = '1'
                    this.powerUList = []
                } else {
                    this.guestDTO.listGuest.isAll = '2'
                }
                if (!this.isAppoint) {
                    this.powerUList = []
                }
                this.guestDTO.guestAgencyBasic.agencyName = this.guestDTO.guestName
               let data = {
                        executivesList: [...this.executivesList],
                        guestAgencyBasic: this.guestDTO.guestAgencyBasic,
                        guestId: '',
                        guestName: this.guestDTO.guestName,
                        listBasic: [this.guestDTO.listBasic],
                        listDepartment: [this.guestDTO.listDepartment], //归属部门
                        listGuest: [this.guestDTO.listGuest],
                        listGuestType: guests, //客户关系 ,
                        listRole: [this.loginer],//权限
                        listStaff: [this.guestDTO.listStaff], // 归属员工
                        regionEntityList: [] //注册地址、办公地址
                    }
                    //地区处理
                    //if (this.registeredAddress.province) {
                        data.regionEntityList.push(this.registeredAddress)
                    //}
                    //if (this.officeAddress.province) {
                        data.regionEntityList.push(this.officeAddress)
                    //}
                    //高管数据处理
                    if (data.executivesList.length !== 0) {
                        data.executivesList.map( (item,index) => {
                            if (item.executivesName === '') {
                                data.executivesList.splice(index,1)
                            }
                        })
                    }
                //一些重复的字段赋值
                data.listBasic[0].guestName = this.guestDTO.guestName
                data.listGuest[0].guestName = this.guestDTO.guestName
                //格式化为数字
                if (this.guestDTO.listBasic.guestType !== '') {
                    data.listBasic[0].guestType = parseInt(this.guestDTO.listBasic.guestType)
                    data.listGuest[0].guestType = parseInt(this.guestDTO.listBasic.guestType)
                } else {
                    data.listBasic[0].guestType = null
                    data.listGuest[0].guestType = null
                }
                if ( data.listBasic[0].credentialsType !== '') {
                    data.listBasic[0].credentialsType = parseInt(data.listBasic[0].credentialsType)
                } else {
                    data.listBasic[0].credentialsType = null
                }
                if ( data.listBasic[0].guestLevel !== '') {
                    data.listGuest[0].guestLevel = parseInt(data.listGuest[0].guestLevel)
                } else {
                    data.listGuest[0].guestLevel = null
                }
                if (this.powerUList.length !== 0) {
                    let arr = []
                    this.powerUList.map((item)=>{
                        let obj = {}
                        obj.id = ''
                        obj.guestId = ''
                        obj.userId = item.id
                        obj.userDepartment = item.deptId
                        obj.userName = item.userName
                        arr.push(obj)
                    })
                    data.listRole = data.listRole.concat(arr)
                }
                // 时间处理
                if (!data.listBasic[0].guestBirthday) {
                   data.listBasic[0].guestBirthday = new Date()
                }
                if (!data.guestAgencyBasic.setupTime) {
                   data.guestAgencyBasic.setupTime = new Date()
                }
                data.listBasic[0].guestBirthday = this.$moment(data.listBasic[0].guestBirthday).format('YYYY-MM-DD');
                data.guestAgencyBasic.setupTime = this.$moment(data.guestAgencyBasic.setupTime).format('YYYY-MM-DD');
                return data
            },
            back() {
                this.$confirm('是否退出?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$router.push({ name: 'customer-management'})
                },()=>{})
            }
        },
        components: {
            'csRegion': require('__components/region'),//地区
        },
    }
    //根据id排序
    function sortById(a,b) {
        return a.id - b.id
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .clientRep{
        .el-checkbox+.el-checkbox{
            margin-left: 0px!important;
        }
        .el-checkbox {
            margin-right: 30px!important;
        }
    }
    .tagStyle{
        line-height: 45px;
        .tagStyle-tag{
            margin-left:5px;
        }
    }
    .operateBox{
        width:146px;
        height:40px;
        text-align: center;
        /*background: red;*/
        position: fixed;
        top:10%;
        right:10%;
        z-index:3000;
    }
    .padding-set{
        /*margin-top: 20px;*/
        padding:0 5%;
    }
    //客户管理 --公共样式
    .ca-client-topic{
        margin-bottom: 20px;
        div{
            text-align: center;
            span{
                display: inline-block;
                width:100%;
                height:1px;
                background:#000;
            }
        }
    }
    .visitAuthStyle{
        margin-bottom: 30px;
    }
    .selectCommonStyle{
        width:100%;
    }
</style>
